package com.example.administrator.daggerdemo.yujian;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ViewFlipper;


import com.example.administrator.daggerdemo.R;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @项目名 Bot
 * @包名 org.auspeed.bot.view
 * @类名 HorizontalCalendarView
 * @时间 2016/11/20
 * @作者 YJ
 * @描述 TODO:横向滑动日期控件
 */
public class HorizontalCalendarView extends LinearLayout implements GestureDetector.OnGestureListener, AdapterView.OnItemClickListener {

    private ViewFlipper flipper1 = null;
    private GridView gridView = null;
    private GestureDetector gestureDetector = null;

    private int selectPostion;//选中
    private int selectTodayPostion;//默认今天
    private int week;

    private List<Date> dateList;
    private Date firstDate;
    private Date lastDate;

    private CalendarAdapter mAdapter = null;
    private Context context;

    public HorizontalCalendarView(Context context) {
        super(context);
        this.context = context;
    }

    public HorizontalCalendarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        layoutInflater.inflate(R.layout.view_horizontal_calendar, this);
        flipper1 = (ViewFlipper) findViewById(R.id.flipper);
    }

    public void initData() {
        gestureDetector = new GestureDetector(this);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        dateList = DateUtils.getDaysToWeek(simpleDateFormat.format(date));
        firstDate = dateList.get(0);
        lastDate = dateList.get(6);
        mAdapter = new CalendarAdapter(dateList, context);
        addGridView();
        gridView.setAdapter(mAdapter);

        week = DateUtils.getNowWeek(simpleDateFormat.format(date));

        if (week == 0) {
            selectPostion = 6;
            selectTodayPostion = 6;
            mAdapter.setSelectPostion(6);
            mAdapter.setSelectTodayPostion(6);
        } else {
            selectPostion = week - 1;
            selectTodayPostion = week - 1;
            mAdapter.setSelectPostion(week - 1);
            mAdapter.setSelectTodayPostion(week - 1);
        }

        gridView.setSelection(selectPostion);
        flipper1.addView(gridView, 0);
    }

    private void addGridView() {
        LayoutParams params = new LayoutParams(AbsListView.LayoutParams.FILL_PARENT, AbsListView.LayoutParams.WRAP_CONTENT);
        gridView = new GridView(context);
        gridView.setNumColumns(7);
        gridView.setGravity(Gravity.CENTER_VERTICAL);
        gridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
        gridView.setVerticalSpacing(1);
        gridView.setHorizontalSpacing(1);
        gridView.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                return gestureDetector.onTouchEvent(motionEvent);
            }
        });
        gridView.setOnItemClickListener(this);
        gridView.setLayoutParams(params);
    }

    @Override
    public boolean onDown(MotionEvent motionEvent) {
        return false;
    }

    @Override
    public void onShowPress(MotionEvent motionEvent) {
    }

    @Override
    public boolean onSingleTapUp(MotionEvent motionEvent) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent motionEvent) {
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        //标记
        int gvFlag = 0;
        // 向左滑
        if (e1.getX() - e2.getX() > 80) {
            addGridView();

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date currentDate = DateUtils.getSomeday(sdf.format(lastDate), 1);
            dateList = DateUtils.getDaysToWeek(sdf.format(currentDate));
            lastDate = dateList.get(6);
            firstDate = dateList.get(0);
            mAdapter = new CalendarAdapter(dateList, context);
            gridView.setAdapter(mAdapter);

            mAdapter.setSelectPostion(-1);
            mAdapter.setSelectTodayPostion(selectTodayPostion);
            gridView.setSelection(selectPostion);

            gvFlag++;
            flipper1.addView(gridView, gvFlag);
            this.flipper1.setInAnimation(AnimationUtils.loadAnimation(context, R.anim.push_left_in));
            this.flipper1.setOutAnimation(AnimationUtils.loadAnimation(context, R.anim.push_left_out));

            //显示下一个
            this.flipper1.showNext();
            flipper1.removeViewAt(0);
            return true;

        } else if (e1.getX() - e2.getX() < -80) {
            addGridView();

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date currentDate = DateUtils.getSomeday(sdf.format(firstDate), -1);
            dateList = DateUtils.getDaysToWeek(sdf.format(currentDate));
            lastDate = dateList.get(6);
            firstDate = dateList.get(0);
            mAdapter = new CalendarAdapter(dateList, context);
            gridView.setAdapter(mAdapter);

            mAdapter.setSelectPostion(-1);
            mAdapter.setSelectTodayPostion(selectTodayPostion);
            gridView.setSelection(selectPostion);

            gvFlag++;
            flipper1.addView(gridView, gvFlag);
            this.flipper1.setInAnimation(AnimationUtils.loadAnimation(context, R.anim.push_right_in));
            this.flipper1.setOutAnimation(AnimationUtils.loadAnimation(context, R.anim.push_right_out));

            this.flipper1.showPrevious();
            flipper1.removeViewAt(0);
            return true;
        }
        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return this.gestureDetector.onTouchEvent(event);
    }

    public Date getSelectPostionDate() {
        return dateList.get(selectPostion);
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
        selectPostion = position;
        mAdapter.setSelectPostion(position);
        mAdapter.notifyDataSetChanged();
        if (onHCalendarItemClickListener != null) {
            onHCalendarItemClickListener.OnHCalendarItemClick(position, dateList.get(position));
        }
    }

    public void setOnHCalendarItemClickListener(OnHCalendarItemClickListener onHCalendarItemClickListener) {
        this.onHCalendarItemClickListener = onHCalendarItemClickListener;
    }

    private OnHCalendarItemClickListener onHCalendarItemClickListener;

    public interface OnHCalendarItemClickListener {
        void OnHCalendarItemClick(int position, Date date);
    }
}
